package com.fafi.selfmvc.convertor;

import org.springframework.format.Formatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatter implements Formatter<Date> {
	private String datePattern;//日期格式字符串

	private SimpleDateFormat dateFormat;//日期格式类

	public DateFormatter(String datePattern) {
		this.datePattern = datePattern;
		dateFormat = new SimpleDateFormat(datePattern);
	}

	//将字符串日期解析成Date对象
	@Override
	public Date parse(String source, Locale locale) throws ParseException {
		return dateFormat.parse(source);
	}

	//将Date格式化为指定日期字符串,返回目标对象的字符串表示法
	@Override
	public String print(Date date, Locale locale) {
		return dateFormat.format(date);
	}
}
